package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.dao.WarehousingEntryDo;
import com.ruoyi.system.domain.WarehousingEntry;
import com.ruoyi.system.domain.WarehousingEntryInfo;
import com.ruoyi.system.mapper.WarehousingEntryInfoMapper;
import com.ruoyi.system.mapper.WarehousingEntryMapper;
import com.ruoyi.system.request.WarehousingEntryAdd;
import com.ruoyi.system.request.WarehousingEntryListRequest;
import com.ruoyi.system.response.WarehousingEntryInfoDetailList;
import com.ruoyi.system.response.WarehousingEntryList;
import com.ruoyi.system.service.IWarehousingEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 入库单Service业务层处理
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Service
public class WarehousingEntryServiceImpl implements IWarehousingEntryService {
    @Autowired
    private WarehousingEntryMapper warehousingEntryMapper;
    @Autowired
    private WarehousingEntryInfoMapper warehousingEntryInfoMapper;

    /**
     * 查询入库单
     *
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public WarehousingEntry selectWarehousingEntryById(Long id) {
        return warehousingEntryMapper.selectWarehousingEntryById(id);
    }

    /**
     * 查询入库单列表
     *
     * @param request 入库单
     * @return 入库单
     */
    @Override
    public List<WarehousingEntryList> selectWarehousingEntryList(WarehousingEntryListRequest request) {
        String materialId = request.getMaterialId();
        WarehousingEntryDo warehousingEntryDo = new WarehousingEntryDo();
        BeanUtils.copyBeanProp(warehousingEntryDo, request);

        List<String> entryOrderIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(materialId)) {
            // 查询物料在哪些入库单里面
            WarehousingEntryInfo entryInfo = new WarehousingEntryInfoDetailList();
            entryInfo.setMaterialId(materialId);
            List<WarehousingEntryInfo> warehousingEntryInfos = warehousingEntryInfoMapper.selectWarehousingEntryInfoList(entryInfo);
            if (CollectionUtils.isEmpty(warehousingEntryInfos)) {
                return new ArrayList<>();
            }
            entryOrderIds = warehousingEntryInfos.stream().map(WarehousingEntryInfo::getEntryOrderId).distinct().collect(Collectors.toList());
        }
        warehousingEntryDo.setEntryOrderIds(entryOrderIds);
        return warehousingEntryMapper.selectWarehousingEntryList(warehousingEntryDo);
    }

    /**
     * 新增入库单
     *
     * @param warehousingEntry 入库单
     * @return 结果
     */
    @Override
    public int insertWarehousingEntry(WarehousingEntryAdd warehousingEntry) {
        return warehousingEntryMapper.insertWarehousingEntry(warehousingEntry);
    }

    /**
     * 修改入库单
     *
     * @param warehousingEntry 入库单
     * @return 结果
     */
    @Override
    public int updateWarehousingEntry(WarehousingEntry warehousingEntry) {
        return warehousingEntryMapper.updateWarehousingEntry(warehousingEntry);
    }

    /**
     * 批量删除入库单
     *
     * @param ids 需要删除的入库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehousingEntryByIds(String ids) {
        return warehousingEntryMapper.deleteWarehousingEntryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库单信息
     *
     * @param id 入库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehousingEntryById(Long id) {
        return warehousingEntryMapper.deleteWarehousingEntryById(id);
    }
}
